<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Fans_brand extends BaseModel
{
    protected $table = 'fans_brand';
    public $timestamps = false;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 粉丝牌列表
     */
    public function fanspailist(){
         $loginId = session('loginId');
         $room = new Roomnum();
         $roominfo = $room->getOne(['uid'=>$loginId]);
         $where['uid'] = $loginId;
         $where['room'] = $roominfo['num'];
         $fansbrandlist = $this->getList($where,'',['fans_brand_lev'=>"ASC"]);
         $fansbrand = [];
         for ($i=0; $i<6; $i++) {
             if (isset($fansbrandlist[$i])){
                 $fansbrand[$i] = $fansbrandlist[$i];
             }else{
                 $fansbrand[$i] = [];
             }
         }
         return $fansbrand;
    }

    /**
     * 检查开通粉丝是否够权限
     */
    public function checkopenfan($lev){
        $loginId = session('loginId');
        $room = new Roomnum();
        $roominfo = $room->getOne(['uid'=>$loginId]);
        $where['uid'] = $loginId;
        $where['room'] = $roominfo['num'];
        // 查看已开通徽章
        $fansp = $this->getList($where);
        $fanspcount = count($fansp);

        // 判断是否有权限开通下个等级
        if($fanspcount+1!=$lev){
              return ['code'=>400,'msg'=>"没有权限开通本徽章"];
        }

        // 开通粉丝徽章所需对应的fans数量
        $arroption = [
             1 => [
                 'fans' => 5
             ],
             2 => [
                 'fans' => 50
             ],
             3 => [
                 'fans' => 500
             ],
             4 => [
                 'fans' => 1500
             ],
             5 => [
                 'fans' => 10000
             ]
         ];

         // 获取我的关注数
         $wherea['attuid'] = $loginId;
         $attention = new Attention();
         $attentioncount = $attention->getCount($wherea);
         if ($attentioncount<$arroption[$lev]['fans']){
             return ['code'=>400,'msg'=>"你的关注量还未达到要求"];
         }
         return ['code'=>200,'msg'=>'ok','lev'=>$lev,'room'=>$roominfo['num']];
    }

    /**
     * 开通粉丝牌
     */
    public function addfansbrand($req){
        $title = $req['title'];
        $lev   = $req['lev'];
        $res = $this->checkopenfan($lev);
        if ($res['code']==400){
              return $res;
        }
        $insert['fans_brand_title'] = $title;
        $insert['fans_brand_lev']   = $lev;
        $insert['uid']              = session('loginId');
        $insert['fans_brand_icon']  = '';
        $insert['room']             = $res['room'];

        // 判断是否已开通
        $isOpen = $this->getOne(['uid'=>session('loginId'),'fans_brand_lev'=>$lev]);
        if ($isOpen) {
            return ['code'=>400,'msg'=>'您已经开通了此徽章'];
        }
        $res = $this->add($insert);
        if ($res) {
            return ['code'=>200,'msg'=>'您已成功开通了此徽章'];
        }
        return ['code'=>400,'msg'=>'开通失败'];
    }
}